package com.itheima.vmmysql.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itheima.vmmysql.Service.DeleteTableService;
import com.itheima.vmmysql.Service.IndexService;

@Controller
public class DeleteTableController {
    @Autowired
    private DeleteTableService deleteTableService;
    @Autowired
    private IndexService indexService;

    @GetMapping("/deletetable")
    public String showTable(Model model){
        List<String> tableNames = indexService.getTableNames();
        model.addAttribute("tableNames", tableNames);
        return "html/deletetable";
    }

    @PostMapping("/deleteTable")
@ResponseBody
public Map<String, Object> deleteTable(@RequestBody Map<String, String> requestBody) {
    String tableName = requestBody.get("tableName");
    Map<String, Object> response = new HashMap<>();
    try {
        deleteTableService.deleteTable(tableName);
        response.put("success", true);
        response.put("message", "表格" + tableName + "已删除");
        System.out.println("表格" + tableName + "已删除");
    } catch (Exception e) {
        response.put("success", false);
        response.put("message", e.getMessage());
    }
    return response;
}

}
